package com.xidian.edu.cn.seckill.scheduled;

import com.xidian.edu.cn.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.security.acl.LastOwnerException;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SeckillSkuScheduled {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private RedissonClient client;

    private final String UPLOAD_LOCK = "seckill:upload:lock";

    //秒杀商品定时上架功能，在空闲时间如每天凌晨三点左右上架最近三天的秒杀商品
    //TODO 幂等性处理
    @Scheduled(cron = "0 0 3 * * ?")
    public void uploadSeckillSkuLatest3Days(){
        //1、重复上架无需处理

        //获取分布式锁
        RLock lock = client.getLock(UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSkuLastest3Days();
        }finally {
            lock.unlock();
        }




    }





}
